<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="google-site-verification" content="kYQuniuaRZuL9fq0DATfL7extAwcqWG6Ki286ZMcpak">
  <meta name="baidu-site-verification" content="code-MRqS7DOO9x">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha256-DfWjNxDkM94fVBWx1H5BMMp0Zq7luBlV8QRcSES7s+0=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"blogwxb.cn","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.11.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":true,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":false,"nav":null,"activeClass":"gitalk"},"stickytabs":false,"motion":{"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="介绍在Web应用中，实现动画效果的方法比较多，JavaScript 中可以通过定时器 setTimeout 来实现，css3 可以使用 transition 和 animation 来实现，html5 中的 canvas 也可以实现。除此之外，html5 还提供一个专门用于请求动画的 API，即  requestAnimationFrame（rAF），顾名思义就是 “请求动画帧”。 为了深入理解">
<meta property="og:type" content="article">
<meta property="og:title" content="H5动画帧requestAnimationFrame">
<meta property="og:url" content="https://blogwxb.cn/H5%E5%8A%A8%E7%94%BB%E5%B8%A7requestAnimationFrame/index.html">
<meta property="og:site_name" content="会飞的猪9527">
<meta property="og:description" content="介绍在Web应用中，实现动画效果的方法比较多，JavaScript 中可以通过定时器 setTimeout 来实现，css3 可以使用 transition 和 animation 来实现，html5 中的 canvas 也可以实现。除此之外，html5 还提供一个专门用于请求动画的 API，即  requestAnimationFrame（rAF），顾名思义就是 “请求动画帧”。 为了深入理解">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-12-25T03:16:40.000Z">
<meta property="article:modified_time" content="2022-10-07T15:14:02.083Z">
<meta property="article:author" content="wxb">
<meta property="article:tag" content="Html5">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://blogwxb.cn/H5%E5%8A%A8%E7%94%BB%E5%B8%A7requestAnimationFrame/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://blogwxb.cn/H5%E5%8A%A8%E7%94%BB%E5%B8%A7requestAnimationFrame/","path":"H5动画帧requestAnimationFrame/","title":"H5动画帧requestAnimationFrame"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>H5动画帧requestAnimationFrame | 会飞的猪9527</title>
  





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
  <!-- Google Analytics -->
  <script>
  window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
  ga('create', 'UA-XXXXX-Y', 'auto');
  ga('send', 'pageview');
  </script>
  <script async src='https://www.google-analytics.com/analytics.js'></script>
  <!-- End Google Analytics -->

  <!-- Baidu Analytics -->
  <script async src = 'https://zz.bdstatic.com/linksubmit/push.js'></script>
  <!-- End Baidu Analytics -->

<link rel="alternate" href="/atom.xml" title="会飞的猪9527" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">会飞的猪9527</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">32</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">5</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">91</span></a></li><li class="menu-item menu-item-commonweal"><a href="/404" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.</span> <span class="nav-text">介绍</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%B1%8F%E5%B9%95%E7%BB%98%E5%88%B6%E9%A2%91%E7%8E%87"><span class="nav-number">2.</span> <span class="nav-text">屏幕绘制频率</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#CSS-%E5%8A%A8%E7%94%BB%E5%8E%9F%E7%90%86"><span class="nav-number">3.</span> <span class="nav-text">CSS 动画原理</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#setTimeout"><span class="nav-number">4.</span> <span class="nav-text">setTimeout</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#rAF"><span class="nav-number">5.</span> <span class="nav-text">rAF</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%99%A4%E6%AD%A4%E4%B9%8B%E5%A4%96%EF%BC%8CrAF-%E8%BF%98%E6%9C%89%E4%BB%A5%E4%B8%8B%E4%B8%A4%E4%B8%AA%E4%BC%98%E5%8A%BF%EF%BC%9A"><span class="nav-number">5.1.</span> <span class="nav-text">除此之外，rAF 还有以下两个优势：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BC%98%E9%9B%85%E9%99%8D%E7%BA%A7"><span class="nav-number">6.</span> <span class="nav-text">优雅降级</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%8A%80%E5%B7%A7%EF%BC%9A"><span class="nav-number">7.</span> <span class="nav-text">技巧：</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="wxb"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">wxb</p>
  <div class="site-description" itemprop="description">主要是分享自己在Web前端的一些入的坑，脱的坑的经历，也会是分享一些Node后端的知识。</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">91</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/dearDreamWeb" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;dearDreamWeb" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>


  <div class="links-of-blogroll site-overview-item animated">
    <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
      推荐
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://www.ruanyifeng.com/blog/" title="https:&#x2F;&#x2F;www.ruanyifeng.com&#x2F;blog&#x2F;" rel="noopener" target="_blank">阮一峰的网络日志</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://nav.blogwxb.cn/" title="https:&#x2F;&#x2F;nav.blogwxb.cn&#x2F;" rel="noopener" target="_blank">轩辕导航</a>
        </li>
    </ul>
  </div>

        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/dearDreamWeb" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://blogwxb.cn/H5%E5%8A%A8%E7%94%BB%E5%B8%A7requestAnimationFrame/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="wxb">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="会飞的猪9527">
      <meta itemprop="description" content="主要是分享自己在Web前端的一些入的坑，脱的坑的经历，也会是分享一些Node后端的知识。">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="H5动画帧requestAnimationFrame | 会飞的猪9527">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          H5动画帧requestAnimationFrame
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-12-25 11:16:40" itemprop="dateCreated datePublished" datetime="2020-12-25T11:16:40+08:00">2020-12-25</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-10-07 23:14:02" itemprop="dateModified" datetime="2022-10-07T23:14:02+08:00">2022-10-07</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">前端</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <!-- <span id="busuanzi_value_page_pv"></span> -->
      <span id="views_text"></span>
    </span>
</div>


        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h1><p>在Web应用中，实现动画效果的方法比较多，JavaScript 中可以通过定时器 <code>setTimeout</code> 来实现，<code>css3</code> 可以使用 <code>transition</code> 和 <code>animation</code> 来实现，html5 中的 <code>canvas</code> 也可以实现。除此之外，html5 还提供一个专门用于请求动画的 API，即 <code> requestAnimationFrame（rAF）</code>，顾名思义就是 “<code>请求动画帧</code>”。 为了深入理解  <code>rAF</code> 背后的原理（后文的 <code>rAF</code> 均指的是 <code>requestAnimationFrame</code>），我们首先需要了解一下与之相关的几个概念：</p>
<span id="more"></span>
<h1 id="屏幕绘制频率"><a href="#屏幕绘制频率" class="headerlink" title="屏幕绘制频率"></a>屏幕绘制频率</h1><p>即图像在屏幕上更新的速度，也即屏幕上的图像每秒钟出现的次数，它的单位是赫兹(<code>Hz</code>)。 对于一般笔记本电脑，这个频率大概是<code>60Hz</code>， 可以在桌面上 右键 &gt; 屏幕分辨率 &gt; 高级设置 &gt; 监视器 中查看和设置。这个值的设定受屏幕分辨率、屏幕尺寸和显卡的影响，原则上设置成让眼睛看着舒适的值都行。<br><code>当你对着电脑屏幕什么也不做的情况下，显示器也会以每秒60次的频率正在不断的更新屏幕上的图像</code>。为什么你感觉不到这个变化？ 那是因为人的眼睛有<code>视觉停留效应</code>，即前一副画面留在大脑的印象还没消失，紧接着后一副画面就跟上来了，这中间只间隔了<code>16.7ms(1000/60≈16.7)</code>， 所以会让你误以为屏幕上的图像是静止不动的。而屏幕给你的这种感觉是对的，试想一下，如果刷新频率变成1次&#x2F;秒，屏幕上的图像就会出现严重的闪烁，这样就很容易引起眼睛疲劳、酸痛和头晕目眩等症状。</p>
<h1 id="CSS-动画原理"><a href="#CSS-动画原理" class="headerlink" title="CSS 动画原理"></a>CSS 动画原理</h1><p>根据上面的原理我们知道，你眼前所看到图像正在以每秒 <code>60</code> 次的频率绘制，由于频率很高，所以你感觉不到它在绘制。而 <code>动画本质就是要让人眼看到图像被绘制而引起变化的视觉效果，这个变化要以连贯的、平滑的方式进行过渡</code>。 那怎么样才能做到这种效果呢？<br><code>60Hz</code> 的屏幕每<code> 16.7ms 绘制一次</code>，如果在屏幕每次绘制前，将元素的位置向左移动一个像素，即1px，这样一来，屏幕每次绘制出来的图像位置都比前一个要差1px，你就会看到图像在移动；而由于人眼的视觉停留效应，当前位置的图像停留在大脑的印象还没消失，紧接着图像又被移到了下一个位置，这样你所看到的效果就是，图像在流畅的移动。这就是视觉效果上形成的动画。 </p>
<h1 id="setTimeout"><a href="#setTimeout" class="headerlink" title="setTimeout"></a>setTimeout</h1><p>理解了上面的概念以后，我们不难发现，<code>setTimeout</code> 其实就是通过设置一个间隔时间来不断的改变图像的位置，从而达到动画效果的。但我们会发现，利用 <code>seTimeout</code> 实现的动画在某些低端机上会出现卡顿、抖动的现象。 这种现象的产生有两个原因：</p>
<ul>
<li><code>setTimeout</code> 的执行时间并不是确定的。在JavaScript中， setTimeout 任务被放进了异步队列中，<code>只有当主线程上的任务执行完以后，才会去检查该队列里的任务是否需要开始执行</code>，所以 <code>setTimeout 的实际执行时机一般要比其设定的时间晚一些</code>。</li>
<li>刷新频率受 <code>屏幕分辨率</code> 和 <code>屏幕尺寸</code> 的影响，不同设备的屏幕绘制频率可能会不同，而 setTimeout 只能设置一个固定的时间间隔，这个时间不一定和屏幕的刷新时间相同。</li>
</ul>
<p>以上两种情况都会导致 <code>setTimeout 的执行步调和屏幕的刷新步调不一致</code>，从而引起<code>丢帧</code>现象。 那为什么步调不一致就会引起丢帧呢？<br>首先要明白，setTimeout 的执行只是在内存中对元素属性进行改变，这个变化必须要等到屏幕下次绘制时才会被更新到屏幕上。如果两者的步调不一致，就可能会导致中间某一帧的操作被跨越过去，而直接更新下一帧的元素。假设屏幕每隔<code>16.7ms</code>刷新一次，而<code>setTimeout</code> 每隔<code>10ms</code>设置图像向左移动<code>1px</code>， 就会出现如下绘制过程（表格）：</p>
<table>
<thead>
<tr>
<th>时间点</th>
<th>情况</th>
</tr>
</thead>
<tbody><tr>
<td>第 0 ms</td>
<td>屏幕未绘制等待中，setTimeout 也未执行，等待中</td>
</tr>
<tr>
<td>第 10 ms</td>
<td>屏幕未绘制等待中，setTimeout 开始执行并设置元素属性 left&#x3D;1px</td>
</tr>
<tr>
<td>第 16.7 ms</td>
<td>屏幕开始绘制，屏幕上的元素向左移动了 1px， setTimeout 未执行，继续等待中</td>
</tr>
<tr>
<td>第 20 ms</td>
<td>屏幕未绘制，等待中，setTimeout 开始执行并设置 left&#x3D;2px</td>
</tr>
<tr>
<td>第 30 ms</td>
<td>屏幕未绘制，等待中，setTimeout 开始执行并设置 left&#x3D;3px</td>
</tr>
<tr>
<td>第 33.4  ms</td>
<td>屏幕开始绘制，屏幕上的元素向左移动了 3px， setTimeout 未执行，继续等待中</td>
</tr>
<tr>
<td>…</td>
<td>….</td>
</tr>
</tbody></table>
<p>从上面的绘制过程中可以看出，屏幕<code>没有更新 left=2px</code> 的那一帧画面，元素直接从<code>left=1px</code> 的位置跳到了 <code>left=3px</code> 的的位置，这就是丢帧现象，这种现象就会引起动画卡顿。</p>
<h1 id="rAF"><a href="#rAF" class="headerlink" title="rAF"></a>rAF</h1><p>与 setTimeout 相比，<code>rAF 最大的优势</code>是 <code>由系统来决定回调函数的执行时机</code>。具体一点讲就是，<code>系统每次绘制之前会主动调用 rAF 中的回调函数</code>，如果系统绘制率是 <code>60Hz</code>，那么回调函数就每<code>16.7ms</code> 被执行一次，如果绘制频率是<code>75Hz</code>，那么这个间隔时间就变成了 <code>1000/75=13.3ms</code>。换句话说就是，<code>rAF</code> 的执行步伐跟着系统的绘制频率走。<code>它能保证回调函数在屏幕每一次的绘制间隔中只被执行一次，这样就不会引起丢帧现象，也不会导致动画出现卡顿的问题</code>。<br>这个API的调用很简单，如下所示：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">varprogress = <span class="number">0</span>;</span><br><span class="line"><span class="comment">//回调函数</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">render</span>(<span class="params"></span>) &#123;</span><br><span class="line">    progress += <span class="number">1</span>;<span class="comment">//修改图像的位置</span></span><br><span class="line"> </span><br><span class="line">    <span class="keyword">if</span>(progress &lt; <span class="number">100</span>) &#123;</span><br><span class="line">           <span class="comment">//在动画没有结束前，递归渲染</span></span><br><span class="line">           <span class="variable language_">window</span>.<span class="title function_">requestAnimationFrame</span>(render);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//第一帧渲染</span></span><br><span class="line"><span class="variable language_">window</span>.<span class="title function_">requestAnimationFrame</span>(render);</span><br></pre></td></tr></table></figure>
<h2 id="除此之外，rAF-还有以下两个优势："><a href="#除此之外，rAF-还有以下两个优势：" class="headerlink" title="除此之外，rAF 还有以下两个优势："></a>除此之外，rAF 还有以下两个优势：</h2><ol>
<li><strong>CPU节能</strong>：使用 setTimeout 实现的动画，当页面被隐藏或最小化时，setTimeout 仍然在后台执行动画任务，由于此时页面处于不可见或不可用状态，刷新动画是没有意义的，而且还浪费 CPU 资源。而 rAF 则完全不同，<code>当页面处理未激活的状态下</code>，该页面的屏幕<code>绘制任务</code>也会被系统暂停，因此跟着系统步伐走的 rAF 也会停止渲染，<code>当页面被激活时，动画就从上次停留的地方继续执行，有效节省了 CPU 开销</code>。</li>
<li><strong>函数节流</strong>：在高频率事件(resize,scroll 等)中，为了防止在一个刷新间隔内发生多次函数执行，使用 rAF 可保证<code>每个绘制间隔内</code>，函数<code>只被执行一次</code>，这样既能保证流畅性，也能更好的节省函数执行的开销。一个绘制间隔内函数执行多次时没有意义的，因为显示器每16.7ms 绘制一次，多次绘制并不会在屏幕上体现出来。</li>
</ol>
<h1 id="优雅降级"><a href="#优雅降级" class="headerlink" title="优雅降级"></a>优雅降级</h1><p>由于 <code>rAF</code> 目前还存在兼容性问题，而且不同的浏览器还需要带不同的前缀。因此需要通过优雅降级的方式对 <code>rAF</code> 进行封装，优先使用高级特性，然后再根据不同浏览器的情况进行回退，直止只能使用 <code>setTimeout</code> 的情况，因此可以这么写：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">window</span>.<span class="property">requestAnimFrame</span> = (<span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">  <span class="keyword">return</span>  <span class="variable language_">window</span>.<span class="property">requestAnimationFrame</span>       ||</span><br><span class="line">          <span class="variable language_">window</span>.<span class="property">webkitRequestAnimationFrame</span> ||</span><br><span class="line">          <span class="variable language_">window</span>.<span class="property">mozRequestAnimationFrame</span>    ||</span><br><span class="line">          <span class="keyword">function</span>(<span class="params"> callback </span>)&#123;</span><br><span class="line">            <span class="variable language_">window</span>.<span class="built_in">setTimeout</span>(callback, <span class="number">1000</span> / <span class="number">60</span>);</span><br><span class="line">          &#125;;</span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure>
<p>但这种写法没有考虑 cancelAnimationFrame 的兼容性，并且不是所有的设备绘制时间间隔都是1000&#x2F;60，下面的代码是比较全的一个 polyfill</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(!<span class="title class_">Date</span>.<span class="property">now</span>)</span><br><span class="line">    <span class="title class_">Date</span>.<span class="property">now</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;<span class="title function_">returnnewDate</span>().<span class="title function_">getTime</span>(); &#125;;</span><br><span class="line"> </span><br><span class="line">(<span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="string">&#x27;use strict&#x27;</span>;</span><br><span class="line">     </span><br><span class="line">    varvendors = [<span class="string">&#x27;webkit&#x27;</span>,<span class="string">&#x27;moz&#x27;</span>];</span><br><span class="line">    <span class="keyword">for</span>(vari = <span class="number">0</span>; i &lt; vendors.<span class="property">length</span> &amp;&amp; !<span class="variable language_">window</span>.<span class="property">requestAnimationFrame</span>; ++i) &#123;</span><br><span class="line">        varvp = vendors[i];</span><br><span class="line">        <span class="variable language_">window</span>.<span class="property">requestAnimationFrame</span> = <span class="variable language_">window</span>[vp+<span class="string">&#x27;RequestAnimationFrame&#x27;</span>];</span><br><span class="line">        <span class="variable language_">window</span>.<span class="property">cancelAnimationFrame</span> = (<span class="variable language_">window</span>[vp+<span class="string">&#x27;CancelAnimationFrame&#x27;</span>]</span><br><span class="line">                                   || <span class="variable language_">window</span>[vp+<span class="string">&#x27;CancelRequestAnimationFrame&#x27;</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(<span class="regexp">/iP(ad|hone|od).*OS 6/</span>.<span class="title function_">test</span>(<span class="variable language_">window</span>.<span class="property">navigator</span>.<span class="property">userAgent</span>)<span class="comment">// iOS6 is buggy</span></span><br><span class="line">        || !<span class="variable language_">window</span>.<span class="property">requestAnimationFrame</span> || !<span class="variable language_">window</span>.<span class="property">cancelAnimationFrame</span>) &#123;</span><br><span class="line">        varlastTime = <span class="number">0</span>;</span><br><span class="line">        <span class="variable language_">window</span>.<span class="property">requestAnimationFrame</span> = <span class="keyword">function</span>(<span class="params">callback</span>) &#123;</span><br><span class="line">            varnow = <span class="title class_">Date</span>.<span class="title function_">now</span>();</span><br><span class="line">            varnextTime = <span class="title class_">Math</span>.<span class="title function_">max</span>(lastTime + <span class="number">16</span>, now);</span><br><span class="line">            <span class="title function_">returnsetTimeout</span>(<span class="keyword">function</span>(<span class="params"></span>) &#123; <span class="title function_">callback</span>(lastTime = nextTime); &#125;,</span><br><span class="line">                              nextTime - now);</span><br><span class="line">        &#125;;</span><br><span class="line">        <span class="variable language_">window</span>.<span class="property">cancelAnimationFrame</span> = <span class="built_in">clearTimeout</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;());</span><br></pre></td></tr></table></figure>
<blockquote>
<p>原创发布 <a target="_blank" rel="noopener" href="http://www.cnblogs.com/onepixel/p/7078617.html">@一像素</a></p>
</blockquote>
<h1 id="技巧："><a href="#技巧：" class="headerlink" title="技巧："></a>技巧：</h1><ol>
<li>return 可以停止动画帧</li>
<li>window.cancelAnimationFrame(动画名)可以取消动画,<br>例子:<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> frame1 =   <span class="variable language_">window</span>.<span class="title function_">requestAnimationFrame</span>(fn);</span><br><span class="line">    <span class="variable language_">window</span>.<span class="title function_">cancelAnimationFrame</span>(frame1); </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>想要有动画效果，window.requestAnimationFrame()要在函数体内才行<br>例子:<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span> () &#123;</span><br><span class="line">    <span class="keyword">if</span>(.....)&#123;   <span class="comment">// 当满足某条件时，停止动画</span></span><br><span class="line">        <span class="keyword">return</span> ;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="variable language_">window</span>.<span class="title function_">requestAnimationFrame</span>(fn)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<p>总例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//html</span></span><br><span class="line">&lt;div id=<span class="string">&quot;map&quot;</span> ref=<span class="string">&quot;map&quot;</span>&gt;&lt;/div&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">//css</span></span><br><span class="line">#map &#123;</span><br><span class="line">  <span class="attr">position</span>: absolute;</span><br><span class="line">  <span class="attr">width</span>: 300px;</span><br><span class="line">  <span class="attr">height</span>: 180px;</span><br><span class="line">  <span class="attr">background</span>: red;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//js</span></span><br><span class="line"> <span class="title function_">move</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="variable language_">this</span>.$nextTick(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">let</span> mapL = <span class="built_in">parseInt</span>(<span class="variable language_">window</span>.<span class="title function_">getComputedStyle</span>(<span class="variable language_">this</span>.<span class="property">$refs</span>.<span class="property">map</span>).<span class="property">left</span>); <span class="comment">// 获取元素的left值</span></span><br><span class="line">        mapL += <span class="number">1</span>;  <span class="comment">// 每次绘制left值加1</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">$refs</span>.<span class="property">map</span>.<span class="property">style</span>.<span class="property">left</span> = mapL + <span class="string">&quot;px&quot;</span>;</span><br><span class="line">        <span class="comment">// 当位移量加上元素的宽度等于大于屏幕的宽度时，停止运动</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">parseInt</span>(<span class="variable language_">this</span>.<span class="property">$refs</span>.<span class="property">map</span>.<span class="property">style</span>.<span class="property">left</span>) + <span class="variable language_">this</span>.<span class="property">$refs</span>.<span class="property">map</span>.<span class="property">offsetWidth</span> &gt;= <span class="variable language_">window</span>.<span class="property">innerWidth</span>) &#123;</span><br><span class="line">          <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 绘制函数</span></span><br><span class="line">        <span class="variable language_">window</span>.<span class="title function_">requestAnimationFrame</span>(<span class="variable language_">this</span>.<span class="property">move</span>);</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
    </div>

    
    
    
      


    <footer class="post-footer">
          <div class="reward-container">
  <div></div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.png" alt="wxb 微信">
        <span>微信</span>
      </div>
      <div>
        <img src="/images/alipay.png" alt="wxb 支付宝">
        <span>支付宝</span>
      </div>

  </div>
</div>

          <div class="followme">
  <span>欢迎关注我的其它发布渠道</span>

  <div class="social-list">

      <div class="social-item">
        <a target="_blank" class="social-link" href="/atom.xml">
          <span class="icon">
            <i class="fa fa-rss"></i>
          </span>

          <span class="label">RSS</span>
        </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/tags/Html5/" rel="tag"># Html5</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/jquery%E4%B8%AD%E7%9A%84ajax%E3%80%81fetch%E5%92%8Caxios%E7%9A%84%E5%8C%BA%E5%88%AB/" rel="prev" title="jquery中的ajax、fetch和axios的区别">
                  <i class="fa fa-chevron-left"></i> jquery中的ajax、fetch和axios的区别
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/video%E7%9A%84autoplay%E5%9C%A8chrome%E6%B5%8F%E8%A7%88%E5%99%A8%E5%A4%B1%E6%95%88%E7%9A%84%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/" rel="next" title="video的autoplay在chrome浏览器失效的解决方案">
                  video的autoplay在chrome浏览器失效的解决方案 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments gitalk-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">wxb</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <!-- <span id="busuanzi_value_site_uv"></span> -->
        <span id="uv-views"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <!-- <span id="busuanzi_value_site_pv"></span> -->
        <span id="pv-views"></span>
      </span>
    </span>
</div><script src="/js/diy_footer.js"></script>
  <div class="powered-by">
    <div style="display: inline-flex;align-items: center;">
      <img src="/images/copyrihgt.png" alt="wxb 微信" with='20' height='20' style="margin-right: 4px;">
      <a href="https://beian.miit.gov.cn/" target="_blank">浙ICP备2022027687号</a>
    </div>
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.5.0/pjax.min.js" integrity="sha256-3NkoLDrmHLTYj7csHIZSr0MHAFTXth7Ua/DDt4MRUAg=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/medium-zoom/1.0.6/medium-zoom.min.js" integrity="sha256-EdPgYcPk/IIrw7FYeuJQexva49pVRZNmt3LculEr7zM=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/gitalk/1.7.2/gitalk.css" integrity="sha256-AJnUHL7dBv6PGaeyPQJcgQPDjt/Hn/PvYZde1iqfp8U=" crossorigin="anonymous">

<script class="next-config" data-name="gitalk" type="application/json">{"enable":true,"github_id":"dearDreamWeb","repo":"blogwxb.github.io","client_id":"86685a6104f8e6ba5827","client_secret":"7cf015e84bbe755bde20c8aca767977e316304ad","admin_user":"dearDreamWeb","distraction_free_mode":true,"proxy":"https://cors-anywhere.azm.workers.dev/https://github.com/login/oauth/access_token","language":"zh-CN","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/gitalk/1.7.2/gitalk.min.js","integrity":"sha256-Pmj85ojLaPOWwRtlMJwmezB/Qg8BzvJp5eTzvXaYAfA="},"path_md5":"c5df79fcca93b823458728e55a1e6f80"}</script>
<script src="/js/third-party/comments/gitalk.js"></script>

</body>
</html>
